package com.wangsh.splider.web.back.controller;

import java.net.URLEncoder;
import java.util.Collections;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wangsh.splider.common.pojo.ApiResponse;
import com.wangsh.splider.common.pojo.ApiResponseEnum;
import com.wangsh.splider.common.util.ConstatFinalUtil;
import com.wangsh.splider.common.util.PageInfoUtil;
import com.wangsh.splider.controller.BaseController;
import com.wangsh.splider.users.pojo.AAdmins;
import com.wangsh.splider.users.service.IUsersDbService;

/**
 * 	管理员后台Controller
 * 	@author wangsh
 */
@Controller
@RequestMapping("/back/admins/")
public class AdminsBackController extends BaseController
{
	@Resource
	private IUsersDbService usersDbService;
	
	/**
	 * 首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--main--");
		return "/back/admins/main";
	}
	
	/**
	 * 欢迎页面
	 * @return
	 */
	@RequestMapping("/welcome")
	public String welcome()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--welcome--");
		return "/back/admins/welcome";
	}
	
	/**
	 * 管理员列表页面
	 * @return
	 */
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminsList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String id = request.getParameter("id");
		request.setAttribute("id", id);
		condMap.put("id", id);
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AAdmins> response = this.usersDbService.findCondListAdminsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		model.addAttribute("response", response.toJSON());
		return "/back/admins/adminsList";
	}
	
	/**
	 * 退出页面
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request, HttpServletResponse response , HttpSession session)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--logout--");
		String returnUrl = request.getParameter("returnUrl");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 获取用户 */
		AAdmins admins = (AAdmins) this.findObjfromSession(request, "admins");
		if(returnUrl == null)
		{
			returnUrl = "" ; 
		}
		try
		{
			returnUrl = URLEncoder.encode(returnUrl, "UTF-8");
			/* 做一些清除的操作 */
			session.removeAttribute("admins");
			session.removeAttribute("lastLoginTime");
			
			apiResponse.setCode(ApiResponseEnum.INFO_LOGOUT.getStatus()); 
			/* 客户端跳转到登陆页面 */
			String logoutUrl = ConstatFinalUtil.CONFIG_JSON.getString("usersCenter.back.logout.url");
			logoutUrl += "&returnUrl=" + URLEncoder.encode(returnUrl,"UTF-8") ; 
			response.sendRedirect(logoutUrl);
			return null ; 
		} catch (Exception e)
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus()); 
			ConstatFinalUtil.SYS_LOGGER.error("退出报错了;email:{}", admins.getEmail(),e);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		/* 提示信息 */
		session.setAttribute("response", apiResponse.toJSON());
		return null ; 
	}
}
